#形态学

#图像二值化处理
import cv2
import numpy as np

#全局二值化，局部二值化
def threshold():
    img = cv2.imread("surprise.jpg")
    img1 =cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    ret, dst = cv2.threshold(img1, 180, 255, cv2.THRESH_BINARY)

    cv2.imshow("original", img)
    cv2.imshow('gray', img1)
    cv2.imshow("dst", dst)
    cv2.waitKey(0)

#自适应阈值二值化
def adaptiveThreshold():
    img = cv2.imread("surprise.jpg")
    img1 = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    result = cv2.adaptiveThreshold(img1, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 5, 0)
    cv2.imshow("result", result)
    cv2.waitKey(0)

if  __name__ == '__main__':
    threshold()
    adaptiveThreshold()